/*
USAGE:

STEP 1 : Include this Java Script file.
         <script src="<%=chipTracContext%>/jsp/includes/scripts/quickSearch.js"></script>

STEP 2 : write a new method and call this when the page loads.
         <body... onload='init()'/>
                or
         window.onload = init;

STEP 3 : In the above written function create QuickSearch Object
         obj_name = new QuickSearch( select_box_having_values_to_search, quick_search_text_field, false);
         the 3rd parameter is true if the search is case sensitive, false otherwise

STEP 4 : create the QuickSearch text field as following
         <input type="text" name="qs1" class="txfield" size='18' onkeyup='quickSearch(obj_name)'/>
         NOTE : THE 'obj_name' THAT IS SENT AS PARAMETER TO THE QUICKSEARCH FUNCTION WHEN KEYUP EVENT IS FIRED
                IS THE QUICKSEARCH OBJECT RETURNED IN STEP 3

STEP 5 : reset the QuickSearch text field when form is reset. add the following line in the 'reset' method.
         obj_name.resetQuickSearchCntrl()         

*/

function QuickSearch(target_ctrl, quicksearch_ctrl, is_case_sensitive )
{
    this.search = searchForValue
    this.resetQuickSearchCntrl = resetQuickSearchCntrl
    this.initSucces = false;

    if( target_ctrl.type.indexOf('select') == 0)
    {
        this.targetCtrl = target_ctrl;
        this.targetCtrlOptions = this.targetCtrl.options;
        this.isCaseSensitive = is_case_sensitive;
        if( quicksearch_ctrl.type == 'text' )
        {
            this.quickSearchCtrl = quicksearch_ctrl;
            this.initSuccess = true;
        }
    }
    else
    {
        return null;
    }
}

function searchForValue()
{
	var valueFound = false;
    if( undefined == this.targetCtrl || undefined == this.quickSearchCtrl || undefined == this.targetCtrlOptions || !this.initSuccess)
	{
        return;
	}
    this.quickSearchCtrl.style.backgroundColor = '#FFFFee';
    /*if( AlphaNumHyphen( this.quickSearchCtrl.value, this.quickSearchCtrl ) == false )
    {
        this.quickSearchCtrl.style.backgroundColor = '#FF0000';
        return;
    }*/
    // Unselecting all the previous selections
	for( var i = 0; i < this.targetCtrlOptions.length; i++ )
	{
		this.targetCtrlOptions[i].selected = false;
	}

	var quickSearchCtrlValue = this.quickSearchCtrl.value;
	if( !this.isCaseSensitive )
	{
		quickSearchCtrlValue = quickSearchCtrlValue.toLowerCase();
	}
    for( var i = 0; i < this.targetCtrlOptions.length; i++ )
	{
        var targetCtrlOptionValue = this.targetCtrlOptions[i].text;
        var quickSearchCtrlValue = this.quickSearchCtrl.value;

        if( !this.isCaseSensitive )
		{
			targetCtrlOptionValue = targetCtrlOptionValue.toLowerCase();
			quickSearchCtrlValue = quickSearchCtrlValue.toLowerCase();
		}
        if(  escape( targetCtrlOptionValue ).search( escape( quickSearchCtrlValue ) ) > -1 )
		{
			this.targetCtrlOptions[i].selected = true;
			valueFound = true;
			this.quickSearchCtrl.style.backgroundColor = '#FFFFee';
			break;
		}
    }
	if( !valueFound )
	{
		this.quickSearchCtrl.style.backgroundColor = '#FF0000';
	}
}

function quickSearch( qsObj )
{
    qsObj.search();
}

function resetQuickSearchCntrl()
{
    this.quickSearchCtrl.style.backgroundColor = '#FFFFee';
}

// function to check only alphabets, numbers, Underscore & Hyphen
function AlphaNumHyphen( input, control )
{
	var Cont_Value      = input;
	var Cont_Name       = control;

	if ( Cont_Value != "" )
	{
	    var startsWith = Cont_Value.charAt(0);
		for( i = 0 ; i < Cont_Value.length; i++ )
		{
			var str = Cont_Value.charAt(i);
			if ( !( ( str >= "A" && str <= "Z" ) || ( str >= "a" && str <= "z" ) || ( str == " ") || ( str >= 0 && str <=9 ) || ( str == "_" ) || ( str == "-" ) || ( str == "(" ) || ( str == ")" )  ) )
			{
				return false;
			}
		}
	}
	return true;
}